--[[VARIABLE DEFINITION ANOMALY DETECTED, DECOMPILATION OUTPUT POTENTIALLY INCORRECT]]--
-- Decompiled with the Synapse X Luau decompiler.

local ServerOHOH = _G.ServerOHOH;
local ProxyNode = ServerOHOH.load.ProxyNode();
local OHOHEnum = ServerOHOH.load.OHOHEnum();
local DataBaseManager = ServerOHOH.load.DataBaseManager();
DataBaseManager:CreateDataBase(OHOHEnum.data_base.player_data);
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());
function M.new(self,...)
	return M(self,...);
end;


local DefaultPlayerData = ServerOHOH.load.PlayerData();
local function InitData(component, serverPlayer)
	
	if DefaultPlayerData.isTest then
		serverPlayer.__new_player = true;
		return DefaultPlayerData.GetInitData()
	end
	
	local init_save_data = serverPlayer.__init_save_data; --判断玩家是否已经有数据
	if not init_save_data then
		local userId = serverPlayer:GetUserId();
		--从数据库中获取数据
		local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
		
		local tryCount = 0;
		
		--不成功获取数据，重复5次，否则踢出
		while not success do
			tryCount = tryCount + 1;
			if tryCount > 5 then --获取用户数据失败
				serverPlayer:Kick("roblox error,get save data field,please retry!");
				return;
			end;
			print("error try load data again");
			local s, d = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
			success = s;
			userData = d;
			wait(7);		
		end;
		
		init_save_data = userData;
		--新玩家
		if not init_save_data then
			-- print("----》新玩家")
			serverPlayer.__new_player = true;
			init_save_data = DefaultPlayerData.GetInitData(); --给初始数据
		end;
	end;
	return init_save_data;
end;
local function VersionUpdate(real)
	local isUpdate = nil;
	for v = real.version + 1, #DefaultPlayerData.version_update do
		local updFuc=DefaultPlayerData.version_update[v]
		if updFuc then
			updFuc(real)
			real.version = v;
			isUpdate = true;
		else
			print("不存在数据更像版本-->",v)
		end
		-- DefaultPlayerData.version_update[v](real);
		-- real.version = v;
		-- isUpdate = true;
	end;
	return isUpdate;
end;
function M.ctor(self, obj)
	M.super.ctor(self, obj);
	local real = InitData(self, obj);
	if not real then
		return;
	end;


	local isUpdate = VersionUpdate(real);
	
	self.__save_data = ProxyNode.new(real);
	
	DataBaseManager:Save(OHOHEnum.data_base.player_data, obj:GetUserId(), function()
		return self.__save_data.__real;
	end);
	-- self:Print()
end;


function M.SetReal(self, real)

	self.__save_data.__real=real
end


function M.Print(self, p8)
	ServerOHOH:PrintTable(self.__save_data.__real)
end
function M.PrintAll(self, p8)
	ServerOHOH:PrintTable(self.__save_data)
end
function M.InitLevelData(self, p8)
	
end
function M.GetData(self, p8)
	assert(p8, "param data name is nil");
	assert(self.__save_data[p8]~=nil, "no save data named " .. p8);
	return self.__save_data[p8];
end;
function M.SetData(self, key,value)
	self.__save_data[key]=value
end

function M.Execut(self, fuc, ...)
	local v16, v17 = xpcall(fuc, debug.traceback, ...);
	if self.__save_data:Isdirty() then
		if not v16 then --修改数据失败，恢复
		-- print("----数据被恢复--------")
			self.__save_data:Revert();
		else
			self.__save_data:Update(); --更新引用
			-- print("----更新引用-------")

			--  是否存储数据
			if not  DefaultPlayerData.isTest then
				DataBaseManager:Save(OHOHEnum.data_base.player_data, self.parent:GetUserId(), function()
					return self.__save_data.__real;
				end);
			end
			
		end;
		self.__save_data:Clean(); --清除脏状态
	end;
	if v16 then
		return true;
	end;
	warn(v17);
	return false;
end;
function M.dtor(self)

	if self.__save_data and self.__save_data:Isdirty() then
		DataBaseManager:Save(OHOHEnum.data_base.player_data, self.parent:GetUserId(), function()
			return self.__save_data.__real;
		end);
		self.__save_data:Clean();
	end;

	 M.super.dtor(self);

end;
--------------------------LYB---------------------

function M:Clear()
	print("正在清除用户数据:",self.parent:GetUserId())
	DataBaseManager:Clear(OHOHEnum.data_base.player_data, self.parent:GetUserId(), function()
		wait(6)
		self.parent:Kick("Clear Data,out Game!");
	end);
end
return M;
